package Server.controller;

import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.DictionarysDao;
import Server.entity.Diction;
import Server.tool.ImageUtils;
import cn.hutool.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("ImgController")
public class ImgController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DictionarysDao dictionarysDao;
    @RequestMapping("upload")
    @ResponseBody
    public RetResult photoUpload(MultipartFile file) throws Exception {
        Map<String, Object> res = new LinkedHashMap<String, Object>();
        String data = null;
        try {
            if (!file.isEmpty()) {
                try {
                    BASE64Encoder encoder = new BASE64Encoder();
                    // 通过base64来转化图片
                    data = encoder.encode(file.getBytes());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            String req=request.getParameter("project");
            if(req.equals("alibb")){
                Diction diction = dictionarysDao.findByKeyst("alibb");
                diction.setVal(data);
                dictionarysDao.save(diction);
            }else{
                Diction diction = dictionarysDao.findByKeyst("weixin");
                diction.setVal(data);
                dictionarysDao.save(diction);
            }
            res.put("code", "0");
            res.put("msg", "成功");
            Map<String, String> das = new LinkedHashMap<String, String>();
            das.put("src", data);
            res.put("data", das);
        } catch (Exception e) {
            res.put("code", "1");
            res.put("msg", "失败");
        }
        return RetResponse.makeOKRsp(res);
    }
    @Value("${imgHref}")
    private String href;
    @RequestMapping("GetAlibb")
    @ResponseBody
    public void GetAlibb(HttpServletResponse rp) throws IOException {
        Diction diction = dictionarysDao.findByKeyst("alibb");
        BASE64Decoder decoder = new BASE64Decoder();
        // 通过base64来转化图片
        byte[] decodeBuffer = decoder.decodeBuffer(diction.getVal());
        String rdm=UUID.randomUUID().toString();
        OutputStream out = new FileOutputStream(href+"/"+rdm +".jpg");
        out.write(decodeBuffer);
        out.flush();
        out.close();
        String filePath = href+"/"+rdm +".jpg";
        File imageFile = new File(filePath);
        if (imageFile.exists()) {
            FileInputStream fis = null;
            OutputStream os = null;
            try {
                fis = new FileInputStream(imageFile);
                os = rp.getOutputStream();
                int count = 0;
                byte[] buffer = new byte[1024 * 8];
                while ((count = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, count);
                    os.flush();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    @RequestMapping("GetWeixin")
    @ResponseBody
    public void GetWeixin(HttpServletResponse rp) throws IOException {
        Diction diction = dictionarysDao.findByKeyst("weixin");
        BASE64Decoder decoder = new BASE64Decoder();
        // 通过base64来转化图片
        byte[] decodeBuffer = decoder.decodeBuffer(diction.getVal());
        String rdm=UUID.randomUUID().toString();
        OutputStream out = new FileOutputStream(href+"/"+rdm +".jpg");
        out.write(decodeBuffer);
        out.flush();
        out.close();
        String filePath = href+"/"+rdm +".jpg";
        File imageFile = new File(filePath);
        if (imageFile.exists()) {
            FileInputStream fis = null;
            OutputStream os = null;
            try {
                fis = new FileInputStream(imageFile);
                os = rp.getOutputStream();
                int count = 0;
                byte[] buffer = new byte[1024 * 8];
                while ((count = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, count);
                    os.flush();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
